package cn.work.control;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ServiceSearchServlet", value = "/ServiceSearchServlet")
public class ServiceSearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=UTF-8");

        // 读取请求体中的 JSON 数据
        ObjectMapper objectMapper = new ObjectMapper();
        ServiceDeleteRequest deleteRequest = objectMapper.readValue(request.getReader(), ServiceDeleteRequest.class);

        // 获取服务 ID 列表
        List<Integer> serviceIds = deleteRequest.getServiceIds();
        System.out.println("服务 ID 列表: " + serviceIds);

        if (serviceIds == null || serviceIds.isEmpty()) {
            response.getWriter().write("{\"success\": false, \"message\": \"没有选中任何服务\"}");
            return;
        }

        // 执行删除操作（此处暂时假设删除成功）
        boolean success = true;  // 模拟删除操作成功

        // 返回 JSON 响应
        if (success) {
            response.getWriter().write("{\"success\": true, \"message\": \"删除成功\"}");
        } else {
            response.getWriter().write("{\"success\": false, \"message\": \"删除失败，未找到相应的服务\"}");
        }
    }

    // 用于接收客户端传递的 JSON 请求数据
    public static class ServiceDeleteRequest {
        private List<Integer> serviceIds;

        // Getter 和 Setter
        public List<Integer> getServiceIds() {
            return serviceIds;
        }

        public void setServiceIds(List<Integer> serviceIds) {
            this.serviceIds = serviceIds;
        }
    }
}